<template>
  <avue-crud
    :data="data"
    :page="page"
    :option="option"
    v-model="obj"
    @row-save="rowSave"
    @row-update="rowUpdate"
    @on-load="onLoad"></avue-crud>
</template>

<script>
'use strict'
import {getFoods, addFood, updateFood} from '@/api/food'
import api from '@/commons/api'
export default {
  name: 'index',
  data () {
    return {
      obj: {},
      data: [],
      page: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      option: {
        title: '商品信息',
        page: true,
        align: 'center',
        menuAlign: 'center',
        index: true,
        column: [
          {
            label: '菜名',
            prop: 'name'
          },
          {
            label: '类型名称',
            prop: 'typeId',
            type: 'select',
            props: {
              label: 'name',
              value: 'id'
            },
            dicUrl: api.baseURL + '/foodType/{{key}}',
            dicData: 'getFoodTypes'
          },
          {
            label: '价格',
            prop: 'price',
            type: 'number',
            precision: 2
          },
          {
            label: '图片',
            prop: 'url',
            type: 'upload',
            span: 24,
            tip: '只能上传jpg/png文件，且不超过500kb',
            action: api.UploadURL,
            imgWidth: 100,
            listType: 'picture-img'
          },
          {
            label: '销售价格',
            prop: 'salePrice'
          },
          {
            label: '是否打折',
            prop: 'isSale',
            type: 'radio',
            valueDefault: 0,
            dicData: [{
              label: '打折',
              value: 1
            }, {
              label: '不打折',
              value: 0
            }]
          },
          {
            label: '限制数量',
            prop: 'saleCount',
            type: 'number'
          },
          {
            label: '销量',
            prop: 'count',
            addVisdiplay: false,
            editVisdiplay: false,
            viewVisdiplay: false
          },
          {
            label: '赞',
            prop: 'praise',
            addVisdiplay: false,
            editVisdiplay: false,
            viewVisdiplay: false
          }
        ]
      }
    }
  },
  methods: {
    onLoad (page) {
      console.log(page)
      getFoods(null, page.currentPage, page.pageSize).then(res => {
        this.data = res.data.list
        this.page.total = res.data.total
      }).catch(err => {
        console.log(err)
      })
    },
    rowSave (form, done, loading) {
      addFood(this.obj).then(res => {
        this.onLoad(this.page)
        this.$message.success(res.data.msg)
        done()
      }).catch(res => {
        this.$message.error(res)
      })
    },
    rowUpdate (row, index, done, loading) {
      updateFood(this.obj).then(res => {
        this.onLoad(this.page)
        this.$message.success(res.data.msg)
        done()
      }).catch(res => {
        this.$message.error(res)
      })
    },
    onSubmit () {
      console.log('submit!')
    }
  }
}
</script>

<style scoped>

</style>
